//
//  InputViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/3.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit
//import tags
class InputViewController: BaseViewController {

    var bl:changeUserBlock?
    
    var data:[String:Any] = ["key":"","value":""]
    
    var relation_id:Int = 0
    
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var tfInput: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.tfInput.text = self.data["value"] as? String
        self.tfInput.placeholder = "请输入\(self.title!)"
        
        self.textView.placeholder = "请输入\(self.title!)（小于200个字）"
        if self.data["key"] as! String == "user_remark" {
            self.textView.isHidden = false
            self.tfInput.isHidden = true
            viewBorderRadius(cor: 4, img: self.textView, with: 1, color: .lightGray)
            self.loadData()
        }else {
            self.textView.isHidden = true
            self.tfInput.isHidden = false
        }
        
        // Do any additional setup after loading the view.
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "保存", style: .plain, target: self, action: #selector(self.save(_:)))
    }
    
    
    func loadData() -> Void {
        NetWorkRequest(.relationMark(enterprise_id: self.data["value"] as! String)) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                let jd = jsonFormater(data: res["data"] as Any)
                self.relation_id = jd["id"].intValue
                self.textView.text = jd["remark"].stringValue
            }
        }
    }
    
    
    func updateRemark() -> Void {
//        if self.textView.text.isEmpty {
//            showMessage(message: "请先输入备注")
//            return
//        }
        
        if self.textView.text.count > 200 {
            showMessage(message: "备注过长")
            return
        }
        showHUD()
        NetWorkRequest(.relationMarkPost(enterprise_id: "\(self.relation_id)", relation_remark: self.textView.text)) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                showMessage(message: "保存成功")
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    @objc func save(_ bar : UIBarButtonItem) {
//        self.data["value"] = self.tfInput.text!
        
        tfInput.text = tfInput.text?.replacingOccurrences(of: " ", with: "")
        tfInput.text = tfInput.text?.replacingOccurrences(of: "\n", with: "")
        
        if self.textView.isHidden == false {
            updateRemark()
            return
        }
        
        
        if (self.data["key"] as! String).contains("phone") {
            if self.validFormatMobile(self.tfInput.text!) == false {
                showMessage(message: "输入内容格式不正确")
                return
            }
        }
        if (self.data["key"] as! String).contains("identity_card") {
            if self.validIdCard(self.tfInput.text!) == false {
                showMessage(message: "输入内容格式不正确")
                return
            }
        }
        
        if (self.data["key"] as! String).contains("processing_daily_capacity")
            || (self.data["key"] as! String).contains("depot_daily_ability") {
            let s = self.tfInput.text!
            if s.contains(".") {
                let s1 = String(s.split(separator: ".").last!)
                if s1.count > 4 {
                    showMessage(message: "\(self.title!)的小数位数不能超过4位")
                    return
                }
            }
        }
        
        
        self.bl?(["key":self.data["key"] as! String,"value":self.tfInput.text!])
        self.navigationController?.popViewController(animated: true)
    }

    
    func validFormatMobile(_ s: String) -> Bool {
        let regex = "^[1][0-9]{10}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: s)
    }
    
    func validIdCard(_ s: String) -> Bool {
        let regex = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: s)
    }
    
    func getStringByRangeIntValue(Str : NSString,location : Int, length : Int) -> Int{
        
        let a = Str.substring(with: NSRange(location: location, length: length))
        
        let intValue = (a as NSString).integerValue
        
        return intValue
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
